package controllers;

import play.*;
import play.mvc.*;

import models.EEntity;
import models.Topic;
import models.User;
import models.Tag;

import models.*;

/**
 * 
 * @author Shady Emil
 * @version 1.0
 * 
 */
public class TagController extends Controller {
	
	/**
	 * The addTag() action method given a new tag (tagName) will add it to a
	 * given topic (topicID) Then it will print on the screen whether it was
	 * successful or not by calling the tagSplash.html.
	 *
	 * @param topicID
	 *           The id of the topic the tag will be added to
	 * @param tagName
	 *           The name of the tag to be added
	 */
	public static void addTag(long topicID, String tagName) {
		Topic topic;
		EEntity entity;
		User user;
		
		String message;
		
		topic = Topic.findById(topicID);
		entity = topic.entity;
		
		String userName = session.get("username");
		
		if (userName != null) {
			user = User.find("byUserName", userName).first();
			
			if (Topic.assignTag(user.id, entity.id, topic.id, tagName)) {
				message = "Successfuly added the tag " + tagName + " to"
				+ " the topic: " + topic.name;
			} else {
				message = "An error occured while adding the Tag.";
			}
			
		} else {
			message = "An error occured while adding the Tag.";
		}
		
		render("tags/tagSplash.html", message);
	}
	
	//for testing only will be removed
	public static void addTagTest(long topicID, long userID,String tagName) {
		Topic topic;
		EEntity entity;
		
		String message;
		
		topic = Topic.findById(topicID);
		entity = topic.entity;
		
		
		
		if (Topic.assignTag(userID, entity.id, topic.id, tagName)) {
			message = "Successfuly added the tag " + tagName + " to"
			+ " the topic: " + topic.name;
		} else {
			message = "An error occured while adding the Tag.";
		}
		
		render("tags/tagSplash.html", message);
	}
	
	
	/**
	 * The removeTag() action method given a tag (tagID) will remove it from a
	 * given topic (topicID) Then it will print on the screen whether it was
	 * successful or not by calling the tagSplash.html.
	 *
	 * @param topicID
	 *           The id of the topic from which the tag will be removed
	 * @param tagID
	 *           The id of the tag which will be removed
	 */
	public static void removeTag(long topicID, long tagID) {
		Topic topic;
		EEntity entity;
		User user;
		Tag tag;
		
		String message;
		
		topic = Topic.findById(topicID);
		entity = topic.entity;
		tag = Tag.findById(tagID);
		
		String userName = session.get("username");
		user = User.find("byUserName", userName).first();
		
		if (userName != null) {
			
			if (Topic.unassignTag(user.id, entity.id, topic.id, tag.id)) {
				message = "Successfuly deleted the tag " + tag.name + " from"
				+ " the topic: " + topic.name;
			} else {
				message = "An error occured while deleting the Tag.";
			}
			
		} else {
			message = "An error occured while deleting the Tag.";
		}
		
		render("tags/tagSplash.html", message);
	}
	
	//for testing only will be removed
	public static void removeTagTest(long userID, long topicID, long tagID) {
		Topic topic;
		EEntity entity;
		Tag tag;
		
		String message;
		
		topic = Topic.findById(topicID);
		entity = topic.entity;
		tag = Tag.findById(tagID);
		
		
		if (Topic.unassignTag(userID, entity.id, topic.id, tag.id)) {
			message = "Successfuly deleted the tag " + tag.name + " from"
			+ " the topic: " + topic.name;
		} else {
			message = "An error occured while deleting the Tag.";
		}
		
		render("tags/tagSplash.html", message);
	}
	
	
	/**
	 * The renameTag() action method given a tag (tagID) will rename it to
	 * (newName) in a given topic (topicID) Then it will print on the screen
	 * whether it was successful or not by calling the tagSplash.html.
	 *
	 * @param topicID
	 *            The id of the topic from which the tag will be renamed
	 * @param tagID
	 *			  The id of the tag which will be renamed
	 * @param newName
	 *			  The new name of the tag which will be renamed
	 */
	public static void renameTag(long topicID, long tagID, String newName) {
		Topic topic;
		EEntity entity;
		User user;
		Tag tag;
		
		String message;
		
		topic = Topic.findById(topicID);
		entity = topic.entity;
		tag = Tag.findById(tagID);
		
		String userName = session.get("username");
		user = User.find("byUserName", userName).first();
		
		if (userName != null) {
			
			if (Topic.renameTag(user.id, entity.id, topic.id, tag.id, newName)) {
				message = "Successfuly renamed the tag " + tag.name + " in"
				+ " the topic: " + topic.name + " to the name: "
				+ newName;
			} else {
				message = "An error occured while renaming the Tag.";
			}
			
		} else {
			message = "An error occured while renaming the Tag.";
		}
		
		render("tags/tagSplash.html", message);
	}
	
	//for testing only will be removed
	public static void renameTagTest(long userID, long topicID, long tagID, String newName) {
		Topic topic;
		EEntity entity;
		Tag tag;
		
		String message;
		
		topic = Topic.findById(topicID);
		entity = topic.entity;
		tag = Tag.findById(tagID);
		
		if (Topic.renameTag(userID, entity.id, topic.id, tag.id, newName)) {
			message = "Successfuly renamed the tag " + tag.name + " in"
			+ " the topic: " + topic.name + " to the name: "
			+ newName;
		} else {
			message = "An error occured while renaming the Tag.";
		}
		
		
		render("tags/tagSplash.html", message);
	}
	
}